using System.Collections.Generic;
using ManagedUIFlow.Infrastructure.Flows;
using ManagedUIFlow.Infrastructure.Services;
using Xunit;

namespace ManagedUIFlow.Specs.Infrastructure.Services
{
    public class FlowServiceSpecs
    {
        [Fact]
        public void GetFlowDefinition_Returns()
        {
            var service = new FlowService
                              {
                                  Flows = new List<Flow>
                                              {
                                                  new Flow("foo", "foo", RedirectBehavior.Auto, "")
                                              }
                              };

            var flowDefinition = service.GetFlowDefinition("foo");

            Assert.NotNull(flowDefinition);
            Assert.Equal(((List<Flow>)service.Flows)[0].Name, flowDefinition.Name);
        }

        [Fact]
        public void GetFlowDefinition_ReturnsNull()
        {
            var service = new FlowService
            {
                Flows = new List<Flow>
                                              {
                                                  new Flow("foo", "foo", RedirectBehavior.Auto, "")
                                              }
            };

            var flowDefinition = service.GetFlowDefinition("foobar");

            Assert.Null(flowDefinition);            
        }

        [Fact]
        public void GetNavigationForEmptyState_Returns()
        {
            var service = new FlowService
            {
                Flows = new List<Flow>
                                              {
                                                  new Flow("foo", "foo", RedirectBehavior.Auto, "")
                                              }
            };
            var flowDefinition = service.GetFlowDefinition("foo");

            var navigation = service.GetNavigationFor(flowDefinition.Name, flowDefinition.StartupNavigation, "");

            Assert.Equal(flowDefinition.StartupNavigation, navigation);
        }

        [Fact]
        public void GetNavigationForState_Returns()
        {
            var service = new FlowService
                              {
                                  Flows = new List<Flow>
                                              {
                                                  new Flow("foo", "foo", RedirectBehavior.Auto, "")
                                                      {
                                                          StartupNavigation = "process",
                                                          Navigations = new List<Navigation>
                                                                            {
                                                                                new Navigation("process",
                                                                                               NavigationType.RenderView)
                                                                                    {
                                                                                        Transitions =
                                                                                            new List<Transition>
                                                                                                {
                                                                                                    new Transition(
                                                                                                        "submit",
                                                                                                        "display")
                                                                                                }
                                                                                    }

                                                                            }
                                                      }
                                              }
                              };
            var flowDefinition = service.GetFlowDefinition("foo");

            var navigation = service.GetNavigationFor(flowDefinition.Name, flowDefinition.StartupNavigation, "submit");

            Assert.Equal("display", navigation);
        }

        [Fact]
        public void GetNavigationForInvalidState_Doesnothing()
        {
            var service = new FlowService
            {
                Flows = new List<Flow>
                                              {
                                                  new Flow("foo", "foo", RedirectBehavior.Auto, "")
                                              }
            };
            var flowDefinition = service.GetFlowDefinition("foo");

            var navigation = service.GetNavigationFor(flowDefinition.Name, flowDefinition.StartupNavigation, "submit");

            Assert.Equal("foo", navigation);
        }

    }
}